package com.iespring.service.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.order.RefundBO;
import com.iespring.bean.admin.order.bo.OrderListBo;
import com.iespring.bean.admin.order.bo.ShipBO;
import com.iespring.bean.admin.order.pojo.Order;
import com.iespring.bean.admin.order.pojo.OrderExample;
import com.iespring.bean.admin.order.pojo.OrderGoods;
import com.iespring.bean.admin.order.pojo.OrderGoodsExample;
import com.iespring.bean.admin.order.vo.DetailOrderVO;
import com.iespring.bean.admin.order.vo.OrderResultVo;
import com.iespring.bean.admin.order.vo.UserInfoForDetailOrderVO;
import com.iespring.bean.admin.user.entity.MallUser;
import com.iespring.mapper.admin.MallUserMapper;
import com.iespring.mapper.admin.PromotionOrderGoodsMapper;
import com.iespring.mapper.admin.PromotionOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 浩澄
 * @Date: 2022/12/28-21:34
 * @Mission: #
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    PromotionOrderMapper orderMapper;
    @Autowired
    PromotionOrderGoodsMapper orderGoodsMapper;

    @Autowired
    MallUserMapper userMapper;

    @Override
    public OrderResultVo list(OrderListBo orderListBo) {
        PageHelper.startPage(orderListBo.getPage(), orderListBo.getLimit());

        OrderExample orderExample = new OrderExample();

        if (orderListBo.getOrderStatusArray() != null) {
            orderExample.createCriteria().andOrderStatusIn(orderListBo.getOrderStatusArray());
        }
        //没有传送userid的情况
        if (orderListBo.getUserId() != null) {
            orderExample.createCriteria().andUserIdEqualTo(orderListBo.getUserId());
        }
        if (orderListBo.getOrderSn() != null) {
            orderExample.createCriteria().andOrderPriceEqualTo(BigDecimal.valueOf(orderListBo.getOrderSn()));
        }
        orderExample.createCriteria().andDeletedEqualTo(false);

        List<Order> orders = orderMapper.selectByExample(orderExample);
        for (Order order : orders) {
            order.setPayId(null);
            order.setPayTime(null);
            order.setRefundAmount(null);
            order.setRefundType(null);
            order.setRefundContent(null);
            order.setRefundTime(null);
            order.setConfirmTime(null);
            order.setAddTime(null);
        }

        PageInfo info = new PageInfo(orders);

        OrderResultVo orderResultVo = new OrderResultVo();

//        orderResultVo.setTotal(orders.size());
        orderResultVo.setTotal(info.getTotal());
//        orderResultVo.setPages(1);
        orderResultVo.setPages(info.getPages());
//        orderResultVo.setLimit(orderListBo.getLimit());
        orderResultVo.setLimit(info.getPageSize());
//        orderResultVo.setPage(1);
        orderResultVo.setPage(info.getPageNum());
        orderResultVo.setList(orders);

        return orderResultVo;
    }

    @Override
    public DetailOrderVO detail(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();

        orderGoodsExample.createCriteria().andOrderIdEqualTo(order.getId());

        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        MallUser user = userMapper.selectByPrimaryKey(order.getUserId());

        UserInfoForDetailOrderVO userInfoForDetailOrderVO = new UserInfoForDetailOrderVO();

        userInfoForDetailOrderVO.setUsername(user.getUsername());

        userInfoForDetailOrderVO.setAvatar(user.getAvatar());

        order.setPayId(null);
        order.setPayTime(null);
        order.setShipSn(null);
        order.setShipChannel(null);
        order.setShipTime(null);
        order.setRefundAmount(null);
        order.setRefundType(null);
        order.setRefundContent(null);
        order.setRefundTime(null);
        order.setConfirmTime(null);
        order.setEndTime(null);

        DetailOrderVO detailOrderVO = new DetailOrderVO();
        detailOrderVO.setOrder(order);
        detailOrderVO.setOrderGoods(orderGoods);
        detailOrderVO.setUser(userInfoForDetailOrderVO);

        return detailOrderVO;

    }

//    @Override
//    public int delete(Integer orderId, HttpServletRequest request) {
//        //根据订单id获取订单编号
//        Order orderSn = orderMapper.selectByPrimaryKey(orderId);
//        //request.setAttribute("orderSn", orderSn.getOrderSn());
//
//        //新的操作
//        //订单的状态用list存
//        List<Integer> integers = new ArrayList<>();
//        integers.add(102);
//        integers.add(103);
//        integers.add(203);
//        integers.add(401);
//        integers.add(402);
//
//        int row=0;
//
//        boolean contains = integers.contains((int) orderSn.getOrderStatus());
//        if (contains){
//            orderSn.setDeleted(true);
//            row = orderMapper.updateByPrimaryKeySelective(orderSn);
//        }
//        if (row == 1){
//            return 200;
//        }else {
//            return 621;
//        }
//    }

    @Override
    public int delete(Integer orderId) {
        //根据订单id获取订单编号
        Order orderSn = orderMapper.selectByPrimaryKey(orderId);
        //request.setAttribute("orderSn", orderSn.getOrderSn());

        //新的操作
        //订单的状态用list存
        List<Integer> integers = new ArrayList<>();
        integers.add(102);
        integers.add(103);
        integers.add(203);
        integers.add(401);
        integers.add(402);

        int row=0;

        boolean contains = integers.contains((int) orderSn.getOrderStatus());
        if (contains){
            orderSn.setDeleted(true);
            row = orderMapper.updateByPrimaryKeySelective(orderSn);
        }
        if (row == 1){
            return 200;
        }else {
            return 621;
        }
    }

//    @Override
//    public void ship(ShipBO shipBO, HttpServletRequest request) {
//        //更改订单状态,添加发货信息
//        Order order = new Order();
//
//        order.setId(shipBO.getOrderId());
//        order.setOrderStatus((short) 301); //表示已经发货
//        order.setShipChannel(shipBO.getShipChannel());
//        order.setShipSn(shipBO.getShipSn());
//        order.setShipTime(new Date());
//
//        orderMapper.updateByPrimaryKeySelective(order);
//
//        Order orderSn = orderMapper.selectByPrimaryKey(order.getId());
//        request.setAttribute("orderSn", orderSn.getOrderSn());
//    }

    @Override
    public void ship(ShipBO shipBO) {
        //更改订单状态,添加发货信息
        Order order = new Order();

        order.setId(shipBO.getOrderId());
        order.setOrderStatus((short) 301); //表示已经发货
        order.setShipChannel(shipBO.getShipChannel());
        order.setShipSn(shipBO.getShipSn());
        order.setShipTime(new Date());

        orderMapper.updateByPrimaryKeySelective(order);

    }

//    @Override
//    public void refund(RefundBO refundBO, HttpServletRequest request) {
//        //更改订单状态,添加退款信息
//        Order order = new Order();
//
//        order.setId(refundBO.getOrderId());
//        order.setOrderStatus((short) 203); //表示已退款
//        order.setRefundAmount(refundBO.getRefundMoney());
//        order.setShipTime(new Date());
//
//        orderMapper.updateByPrimaryKeySelective(order);
//
//        Order orderSn = orderMapper.selectByPrimaryKey(order.getId());
//        request.setAttribute("orderSn", orderSn.getOrderSn());
//
//    }
    @Override
    public void refund(RefundBO refundBO) {
        //更改订单状态,添加退款信息
        Order order = new Order();

        order.setId(refundBO.getOrderId());
        order.setOrderStatus((short) 203); //表示已退款
        order.setRefundAmount(refundBO.getRefundMoney());
        order.setShipTime(new Date());

        orderMapper.updateByPrimaryKeySelective(order);

    }
}
